<?php
// TODO: WIP
// TODO: TOOOO Much code in here.  Must remove code.  Code must die!

class XmlDatasource {

	function XmlDatasource( $oXmlDocument ) {
		$this->oXmlDocument = $oXmlDocument;
	}
		
	function GetTableList() {
		$oTableElements = XmlSearcher::GetElements( $this->oXmlDocument, 'TableList>>Table' );
		return $oTableElements->ReturnArrayFromCallback( 'GetAttribute', 'Name' );
	}
	
	function _GetColumnsFromTable( $oXmlTableElement ) {
		$oColumnElements = XmlSearcher::GetElements( $oXmlTableElement, 'Column' );
		return $oColumnElements->ReturnArrayFromCallback( 'GetAttributesAsAssociativeArray', array( 'NAME' => 'Name', 'DATATYPE' => 'Datatype' ) );
	}

	function GetColumnList( $sTableName ) {
		$sTableName = strtoupper( $sTableName );

		$oTableElements = XmlSearcher::GetElements( $this->oXmlDocument, 'TableList>>Table' );
		
		$aTableNames = array();
		$oTableElements->RewindElementIterator();
		while( $oTableElements->HasAnotherElement() ) {
			$oThisTable = $oTableElements->GetNextElement();
			if (  strtoupper( $oThisTable->GetAttribute( 'Name' ) ) == $sTableName ) {
				return $this->_GetColumnsFromTable( $oThisTable );
			}
		}
		
		return $aTableNames;
	}
	
	function GetForeignKeys( $sTableName ) {
		$sTableName = strtoupper( $sTableName );
		
		$oForeignKeyElements = XmlSearcher::GetElements( $this->oXmlDocument, 'ForeignKeyList>>ForeignKey' );
		
		// TODO: GetElementsWithAttribute( $sName, $sValue )??
		$aForeignKeys = array();
		while( $oForeignKeyElements->HasAnotherElement() ) {
			$oThisKey = $oForeignKeyElements->GetNextElement();
			if ( strtoupper( $oThisKey->GetAttribute( 'FromTable') ) == $sTableName || strtoupper( $oThisKey->GetAttribute( 'ToTable' ) == $sTableName ) ) {
				$aThisKey = $oThisKey->GetAttributesAsAssociativeArray( array( 'NAME' 		=> 'Name'
																			 , 'FROM_TABLE'	=> 'FromTable'
																			 , 'TO_TABLE'   => 'ToTable'
																			 ) );
				$aThisKey['FROM_COLUMNS'] = array();  // TODO: sort out what goes here
				$aThisKey['MANDATORY']    = ( strtolower( $oThisKey->GetAttribute( 'Mandatory' ) ) =='true' );
				$aForeignKeys[] = $aThisKey;
			}
		}
		return $aForeignKeys;
	}
	
	function GetPrimaryKey( $sTableName ) {
		return array();
		/*
		$aKey = array();
		
		$oKeyElement = XmlSearcher::GetElements( $oXmlTableElement, 'PrimaryKey' );
		
		$oColumnElements = XmlSearcher::GetElements( $oXmlTableElement, 'PrimaryKey>>PrimaryKeyColumn' );
		
		$oColumnElements->RewindElementIterator();
		while( $oColumnElements->HasAnotherElement() ) {
			$oThisColumn = $oColumnElements->GetNextElement();
			$aKey['COLUMNS'][]  = $oThisColumn->GetAttribute( 'Name' );  // Not yet in the output of the XMLRenderer	
		}
		return $aKey;
		*/		
	}
	
	function GetDatasourceId() {
		return $this->oXmlDocument->GetName();
	}

	function GetLastDdl() {
		return time();
	}	
	// TODO: Parent class for all these things
	function IsConnected() {
		return true;
	}

	function Errored() {
		return false;
	}

	function GetLastError() {
		return '';
	}

}

?>
